package com.w2cx.selenium.base;




import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.ITestResult;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class DriverBase{
   public static WebDriver driver;
    public DriverBase(String browserName){
        SelectDriver selectDriver = new SelectDriver();
        driver = selectDriver.generateDriver(browserName);
    }

    private static int WAIT_TIME = 10;

    //封装driver关闭方法

    public  void stopDriver(){
        driver.close();
    }

    /**
     *
     * 封装driver获取单元素方法
     * **/
    public WebElement findElement(By by){
        //等待
        WebDriverWait wait = new WebDriverWait(driver,WAIT_TIME);
        wait.until(ExpectedConditions.visibilityOfElementLocated(by));

        WebElement element = driver.findElement(by);
        return element;
    }


    //封装driver获取元素列表的方法
    public List<WebElement> findElements(By by){
        //等待
        WebDriverWait wait = new WebDriverWait(driver,WAIT_TIME);
        wait.until(ExpectedConditions.visibilityOfElementLocated(by));

        List<WebElement> list = driver.findElements(by);
        return  list;
    }



    //封装driver的单击方法
    public void click(WebElement element){
        //等待
        WebDriverWait wait = new WebDriverWait(driver,WAIT_TIME);
        wait.until(ExpectedConditions.visibilityOf(element));

        element.click();
    }

    //双击操作
    public void doubleClick(WebElement element){
        Actions actions = new Actions(driver);
        actions.doubleClick(element).perform();

    }


    //action的sendKeys
   /* public void action_sendKeys(){
        Actions actions = new Actions(driver);
        actions.sendKeys()
    }
*/


    //封装driver的输入方法
    public void sendKeys(WebElement element,String text){
        //等待
        WebDriverWait wait = new WebDriverWait(driver,WAIT_TIME);
        wait.until(ExpectedConditions.visibilityOf(element));

        //输入
        element.clear();
        element.sendKeys(text);
    }




    //封装driver的判断某元素是否显示的方法
    public boolean isDisplayed(WebElement element){
        //等待
        WebDriverWait wait = new WebDriverWait(driver,WAIT_TIME);
        wait.until(ExpectedConditions.visibilityOf(element));
        //判断
        return element.isDisplayed();
    }


    //封装driver的get方法
    public  void  get(String url){
        driver.get(url);
    }

    //封装driver的窗口最大化方法
    public void manage(){
        driver.manage().window().maximize();
    }


    //获取cookie方法
    public  Set<Cookie>  getCookie(){
        Set<Cookie> cookies = driver.manage().getCookies();
        return cookies;
    }


    //设置cookie方法
    public void setCookie(Cookie cookie){
        driver.manage().addCookie(cookie);
    }



    //强制刷新方法
    public void refresh(){
        driver.navigate().refresh();
       /* Actions ctrl = new Actions(driver);
        ctrl.keyDown(Keys.CONTROL).perform();
        try {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_F5);
            ctrl.keyUp(Keys.CONTROL).perform();
        } catch (AWTException e) {
            e.printStackTrace();
        }*/
    }







      //截图方法
    public static void screenShot(ITestResult result){
        File file = ((TakesScreenshot)DriverBase.driver).getScreenshotAs(OutputType.FILE);
        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = result.getName()+"_"+format.format(now)+".png";
        try {
            FileUtils.copyFile(file, new File("./screenshots/"+fileName));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }



}
